עקרונות שפות תכנות 2016 תרגול 8 Type Inference System. Substitution Interpreter.

Size: px
Start display at page:

Download "עקרונות שפות תכנות 2016 תרגול 8 Type Inference System. Substitution Interpreter."

Transcription

1 עקרונות שפות תכנות 2016 תרגול 8 Type Inference System. Substitution Interpreter. Type Inference System The Type Inference System is a Scheme Implementation of the algorithm for Type Checking and Inference using Type Constraints presented in Section 5.5 of the Course Book. System Architecture The system is built from the following modules: The layers of the system are: - Abstract Syntax for Scheme Expressions and for Type Expressions. - The key data types manipulated in the algorithm, Type Equations and Type Substitutions. - The logic of the algorithm is implemented in three functional packages: Scheme-Exp-to-Pool, Scheme-Exp-to-Equations and the top-level algorithm Solve. The key interfaces of each package are the following: 1

2 Layer 1: Languages Definitions The Type Inference algorithm maps expressions in one language (Scheme expressions) into expressions in another language (Type Expressions). The lower level of the Type Inference system implements the Abstract Syntax interfaces for expressions in these two languages. Scheme-AST: Implements parser and abstract syntax for the following BNF concrete syntax: BNF <scheme-expression> := <scheme-exp > <scheme-define> <scheme-exp> := <scheme-atomic> <scheme-composite> <scheme-define> := '(' 'define' <scheme-var> <scheme-exp> ')' <scheme-atomic> := <scheme-var> <number> <bool> <scheme-composite> := <scheme-proc> <scheme-app> <scheme-let> <scheme-let*> <scheme-letrec> <scheme-if> <scheme-proc> := '(' 'lambda' '(' <scheme-var>* ')' <scheme-exp>+ ')' <scheme-app> := '(' <scheme-exp>+ ')' <scheme-let> := '(' 'let' '(' <scheme-binding>* ')' <scheme-exp>+ ')' <scheme-let*> := '(' 'let*' '(' <scheme-binding>* ')' <scheme-exp>+ ')' <scheme-letrec> := '(' 'letrec' '(' <scheme-binding>* ')' <scheme-exp>+ ')' <scheme-if> := '(' 'if' <scheme-exp> <scheme-exp> <scheme-exp> ')' <scheme-binding> := '(' <scheme-var> <scheme-exp> ')' Scheme Abstract syntax: Scheme-define: Components var:scheme-var val:scheme-exp Scheme-proc: Components vars:list(scheme-var) body:list(scheme-exp) Scheme-app: Components operator:scheme-exp operands:list(scheme-exp) Scheme-let: Components: bindings:list(pair(scheme-var, Scheme-exp)) body:list(scheme-exp) Scheme-let*: Components: bindings:list(pair(scheme-var, Scheme-exp)) body:list(scheme-exp) Scheme-letrec: Components: bindings:list(pair(scheme-var, Scheme-exp)) body:list(scheme-exp) Scheme-if: Components: test:scheme-exp then:scheme-exp else:scheme-exp Type-Expression-ADT: BNF (Concrete syntax) for Type-Expressions TE -> Atomic-te Composite-te Type-variable Atomic-te -> "Number" "Boolean" Composite-te -> Proc-te Tuple-te Non-Tuple-te -> Atomic-te Proc-te Type-variable Proc-te -> "[" Tuple-te "->" Non-Tuple-te "]" Tuple-te -> (Non-Tuple-te "*")* Non-Tuple-te "Empty" Type-variable -> a symbol starting with an upper case 2

3 Abstract Syntax for Type-Expressions: Tuple-te: Components: List(Non-Tuple-te) Proc-te: Components: Parameters: Tuple-te Return: Non-Tuple-te The parser for Type-Expressions has an element of complexity because it must deal with the precedence of operators among type sub-expressions (* and ->). It uses a parsing algorithm called "shift-reduce" to parse expressions such as [T1 * T2 -> T3] into an expression understood as (-> (* T1 T2) T3) as opposed to (* T1 (-> T2 T3)). The situation is similar to the well-known precedence rules of arithmetic expressions where a parser knows to interpret [2*3+5] as (+ (* 2 3) 5). This logic is implemented in the package Type-Expression-Parser and relies on a Stack data structure to implement the Shift-Reduce algorithm. Layer 2: Substitution and Equation ADTs The Type Inference algorithm is defined in terms of 2 basic formal tools: Type Substitutions are finite mappings of Type Variables to Type Expressions. Equations are pairs that state that a left Type Expression is to be held equivalent to a right Type Expression. Substitution ADT The interface of the Substitution ADT includes: make-sub(variables, type-expressions) sub->expression-of-variable(sub,var): lookup a variable in a substitution extend-sub(sub,var,te) sub-apply(sub,te) sub-combine(sub1,sub2) Equation ADT The interface includes: make-equation(tel, ter) equation->left(eq) equation->right(eq) Layer 3: Scheme-Exp-to-Pool, Scheme-Exp-to-Equations, Solve scheme-exp->pool(exp) traverses the AST of a Scheme Expression and maps each sub-expression in the AST to distinct Type Variables. pool->equations(pool) applies type formulae to propagate constraints from the syntactic structure of the Scheme Expression to constraints among the type variables of the pool. There are specific constraints derived for each type of syntactic construct according to the semantics of the Scheme programming language. infer-type(scheme-expr) applies the whole logic of the type inference algorithm. The main steps of the algorithm are reflected in this top-level fragment: (let* ((pool (scheme-exp->pool scheme-exp)) (equations (pool->equations pool)) (sub (solve-equations equations)) (te (val-of-index scheme-exp pool))) 3

4 The key logic of the algorithm lies in the solve-equations algorithm which turns a list of Type Equations into a single coherent Type Substitution which satisfies all the constraints. Solve() relies on the computation of unification between two type expressions. Exercise: Extend the Type Inference system to support "If" expressions The part of the Type Inference System which "knows" Scheme Expressions is the function make-equation-from-se(se,pool). The structure of this function is a case for each type of Scheme expression. The code we provided supports: Procedure Application Number Boolean Primitive Procedures Extend the code to support Scheme expressions of the type (if <test> <then> <else>). Remember question 2 from last week's practical session. From Typing Rule for if-expressions the type equations derived from a composite if-expression: (if _p _c _a) we derive 2 equations: T c = T a T if = T c Accordingly, the code of the function make-equation-from-se is extended to process if-exp ;; Signature: make-equation-from-se(se,pool) ;; Purpose: Return a single equation ;; Type: [Scheme-expression * Pool -> Equation] ;; Pre-condition: 'se' is a legal Scheme expression, and ;; 'se' is a member of 'pool' (define make-equation-from-se (lambda (se pool) (letrec ((get-tvar-of-exp (lambda (exp) (val-of-index exp pool)))) (cond ;; The type of procedure is (T1 *... * Tn -> Te) ;; where Te is the type of the last exp in the body of the proc. ;; and Ti is the type of each of the parameters. ((scheme-proc? se) (let ((left (get-tvar-of-exp se)) (right (make-proc-te (make-tuple-te (map get-tvar-of-exp (scheme-proc->vars se))) (get-tvar-of-exp (last (scheme-proc->body se)))))) (list (make-equation left right)))) ;; An application must respect the type of its operator ;; Type(Operator) = [T1 *.. * Tn -> Te] ;; Type(Application) = Te ((scheme-app? se) (let ((left (get-tvar-of-exp (scheme-app->operator se))) (right (make-proc-te 4

5 (make-tuple-te (map get-tvar-of-exp (scheme-app->operands se))) (get-tvar-of-exp se)))) (list (make-equation left right)))) ;; Support if-expressions: ;; [if <test> <then> <else>) --> ;; {[T<then> = T<else>], [T<true> = T<if>]} ((scheme-if? se) (let ((Tif (get-tvar-of-exp se)) (Tt (get-tvar-of-exp (scheme-if->then se))) (Te (get-tvar-of-exp (scheme-if->else se)))) (list (make-equation Tif Tt) (make-equation Tt Te)))) ;; The type of a number is Number ((number? se) (let ((left (get-tvar-of-exp se)) (right 'Number)) (list (make-equation left right)))) ;; The type of a boolean is Boolean ((boolean? se) (let ((left (get-tvar-of-exp se)) (right 'Boolean)) (list (make-equation left right)))) ;; The type of a primitive procedure is given by the primitive. ((primitive-procedure? se) (let ((left (get-tvar-of-exp se)) (right (get-primitive-type se))) (list (make-equation left right)))) (else (error 'make-equation "Bad expression ~s" se)))))) 5

6 מבנה המעריך מעריך עבור מודל ההחלפה <scheme-expression> Data structures substitution values Utils A Racket lib Core Test Abstract syntax parser Derived expressions איור המעריך מתחיל עם ביטוי scheme ולאחר מכן מבצע derivation ו- evaluation. מנגנון הבדיקות : 1 מבנה המעריך מבוסס מודל ההחלפה. חץ מסמל שימוש של מודול במודול אחר, למשל ה- Core משתמש ב- DS וב- ASP מרכיב חשוב במעריך הוא מנגנון הבדיקות. בדיקות הן חלק בלתי נפרד מפיתוח תוכנה והן הכרחיות, קל וחומר בתוכנה מורכבת כמו מעריך. רכיב הבדיקות במעריך מורכב משתי פרוצדורות עיקריות: test ו- run-tests. חשוב: כאשר משנים או מוסיפים קוד למעריך )או לכל תוכנית(, מריצים את כל הבדיקות! כך נדע שלא פגענו ברכיב שכבר עובד ונבדק בעבר. הפרוצדורה test מקבלת ביטוי וערך צפוי, מעריכה את הביטוי ובודקת האם תוצאת החישוב שווה לתוצאה המצופה. לדוגמה: (test (derive-eval '(* 3 4)) => '(value 12)) הפרוצדורה run-tests מקבלת מספר לא מוגבל מראש של ביטויי,test מריצה אותם, ומסכמת את התוצאות: (run-tests (test (derive-eval '(+ 3 4)) => '(value 7)) (test (derive-eval '(- 3 4)) => '(value -1)) (test (derive-eval '(/ 4 2)) => '(value 2))) כדי להוסיף בדיקה חדשה פשוט נפעיל את הפרוצדורה test עם הפרמטרים הרצויים מתוך אחד ה- run-tests הקיימים, או שנכתוב run-tests בעצמנו. לדוגמה, אנו רוצים להוסיף בדיקה כי המעריך שלנו מעריך את הביטוי "4" לערך 4. אזי נוסיף את השורה הבאה לקובץ הבדיקות: (run-tests (test (derive-eval '4) => '(value 4))) נקודה לדיון: מדוע יש לתייג את תוצאת החישוב ב- (4 '(value ולא ניתן פשוט להחזיר את הערך 4? ליבת המעריך )core( חלקי הליבה החלק המרכזי של ליבת המעריך מורכב משתי פרוצדורות עם הפניה הדדית ביניהן: applicative-eval apply-procedure 6

7 הפרוצדורה applicative-eval מקבלת כפרמטר ביטוי,scheme כאשר סוג הביטוי הוא אחד מהסוגים ברשימה למטה, ועבור כל אחד מהסוגים הללו, מצוינת דרך הפעולה: applicative-eval(exp): atomic?(exp) eval-atomic (exp) special-form?(exp) eval-special-form (exp) evaluator-value?(exp) exp application?(exp) apply-procedure(applicative-eval(operator(exp)), list-of-values(operands(exp))) הפרוצדורה apply-procedure מקבלת שני פרמטרים: הפרוצדורה אותה רוצים להפעיל ואת הפרמטרים עבורה: apply-procedure(evaluated_proc, evaluated_args): primitive-procedure?(evaluated_proc) apply-primitive-procedure(evaluated_proc,evaluate_args) compound-procedure?(evaluated_proc) params procedure-parameters(evaluated_proc) body rename(procedure-body(evaluated_proc)) eval-sequence(substitute(body, params, evaluated_args))) )הקריאה ההדדית ל- applicative-eval היא בתוך.)eval-sequence אם הפרוצדורה היא פרימיטיבית, מפעילים אותה על הפרמטרים )שכבר הוערכו(. אם הפרוצדורה אינה פרימיטיבית, אזי מבצעים rename לכל המשתנים הקשורים בגוף הפרוצדורה. נשים לב כי פעולת ה- rename מתבצעת רק על גוף הפרוצדורה. אחרי פעולת ה- rename, אין יותר צורך לבדוק מיהם המשתנים הקשורים כדי לבצע substitute פשוט מחליפים לפי שם המשתנה. פרוצדורת ה- substitute הנמצאת ב- ge-adt מקבלת את גוף הפרוצדורה, את הפרמטרים של הפרוצדורה ואת הארגומנטים שאיתם הופעלה הפרוצדורה ומייצרת החלפה שתופעל על הגוף. מדוע צריך לעשות rename רק לגוף הפרוצדורה ללא הפרמטרים שהפרוצדורה מקבלת? תזכורת: הבעיה ש- rename פותר. נסתכל על הקוד הבא: 1. ((lambda (x) 2. (lambda (y) 3. (y x))) 4. (lambda (x) 5. (+ x y))) ה- y בשורה 5 הוא משתנה חופשי. כאשר נבצע הערכה של הביטוי כולו, ה- x בשורה 3 יוחלף על ידי ה- lambda בשורות 4-5. ללא,renaming ה- y החופשי בשורה 5 ייקשר ל- y אשר מוגדר בשורה 2. כלומר, הבעיה שלנו נובעת מתוך הלמבדות בגוף הפרוצדורה. המשתנה x המוגדר בשורה 1 מוחלף מיד על ידי הפרמטר ואינו מהווה בעיה. אופרטורים מיוחדים Forms Special )שינוי/הוספת.)Special Form אופרטורים מיוחדים הינם אופרטורים שיש עבורם חוק חישוב מיוחד. בד"כ לא מוסיפים,special form אלא אם אין ברירה אחרת, מדוע? בכל מקרה, נראה דוגמא של הוספת ביטוי חדש לשפה:,case המקבל ביטוי מספרי ובהתאם לערכו מבצע פעולה כלשהי. (case <numeric_expression> (<value0> <consequence0>) (<value1> <consequence1>) (else <consequence_n>)) 7 הצורה התחבירית של ביטוי :case

8 (define fib (lambda (n) (case n (0 0) (1 1) (else (+ (fib (- n 1)) (fib (- n 2))))))) דוגמה לשימוש בביטוי :case תזכורת: and-or-trees לייצוג :ast <case> Predicate AND Consequence-exprs: Ordered. <exp> <exp>+ (define eval-special-form (lambda (exp) (cond ((quoted? 1 exp) (make-value 2 exp)) ((lambda? 1 exp) (eval-lambda 3 exp)) ((definition? 1 exp) (eval-definition 3 exp)) ((if? 1 exp) (eval-if 3 exp)) ((begin? 1 exp) (eval-begin 3 exp)) ((case? 1 exp) (eval-case 3 exp))))) (define special-form? (lambda (exp) (or (quoted? exp) (lambda? exp) (definition? exp) (if? exp) (begin? exp) (case? exp)))) צעד 1: עדכון.eval-special-form צעד 2: עדכן.special-form? צעד 3: כתיבת חוק החישוב המיוחד. ראשית אנו צריכים לקבוע מהו חוק החישוב של :case הערכת ה- control השוואת ערך ה- control לערכי ה- compared לפי הסדר. הנחנו כי הערך הוא מספרי ולכן ההשוואה מתבצעת ע"י "=". 8 פונקציות אשר ניתנות לנו מהממשק של ASP פונקציות אשר ניתנות לנו ממודל מבני הנתונים של המעריך פונקציות אשר נמצאות בליבת המעריך 1 2 3

9 עבור ה- compared הראשון שערכו שווה ל- control, נעריך את ה- actions. נחזיר את הערך של הביטוי האחרון בהן. אם לא קיים,compared אזי נעריך את ה- actions של ה- else. נחזיר את הערך של הביטוי האחרון בהן. (define (eval-case exp) (letrec ((eval-clauses (lambda (control clauses) (cond ((null? clauses) 'unspecified) ((or (case-last-clause? clauses) (= (applicative-eval (case-compared (case-first-clause clauses))) control)) (eval-sequence (case-actions (case-first-clause clauses)))) (else (eval-clauses control (case-rest-clauses clauses))))))) (eval-clauses (applicative-eval (case-control exp)) (case-clauses exp)))) המנתח הסינטקטי ASP Abstract Syntax Parser הממשק ה- ASP הוא ממשק בין קוד התוכנית לבין ה- core. מודול זה כולל פרוצדורות המחלצות את הרכיבים השונים ומזהות את סוג הביטוי. בשלב זה הביטויים לא מוערכים! נמשיך עם דוגמת ה- case ונוסיף אותו כביטוי חדש לשפה. הערה: הוספת תמיכה בביטוי חדש היא חובה ב- ASP, ולא תלויה בשאלה אם הביטוי החדש יהיה ביטוי נגזר או שהוא חלק מן הגרעין של השפה. נכתוב את ה- ADT עבור ביטוי,case הכולל בתוכו גם ADT עבור ה- case-clause. Constructor for case: make-case(control, case-clauses) Type: [Symbol * List(Clause-expression) -> CASE-Expression] Identification predicate: case?(exp) Type: [T > Boolean] Selectors: case-control(case-exp) Type: [CASE-Expression -> Symbol] case-clauses(case-exp) Type: [CASE-Expression -> List(Clause-expression)] Constructor for case clauses: make-case-clause(compared actions) Type: [T * List -> Clause-expression] 9

10 Selectors for case clauses: case-compared(clause) case-actions(clause) case-first-clause(case-exp) case-rest-clauses(case-exp) Identification predicate for case clauses: case-last-clause?(case-exp) דוגמה לשימוש ב- ADT ליצירת ביטוי ה- case שראינו קודם לכן: (make-case 'n (list (make-case-clause '0 '(0)) (make-case-clause '1 '(1)) (make-case-clause 'else '(+ (fib (- n 1)) (fib ( n 2)))))) (define case? (lambda (exp) (tagged-by? exp 'case))) עתה נראה את המימוש: (define make-case (lambda (control case-clauses) (attach-tag (cons control case-clauses) 'case))) (define case-control (lambda (case-exp) (car (get-content case-exp)))) (define case-clauses (lambda (case-exp) (cdr (get-content case-exp)))) (define make-case-clause (lambda (compared actions) (cons compared actions))) (define case-compared car) (define case-actions cdr) (define case-first-clause car) (define case-rest-clauses cdr) (define case-last-clause? (lambda (clauses) (and (null? (cdr clauses)) 10

11 (eq? (case-compared (case-first-clause clauses)) 'else)))) ביטויים נגזרים Sugar( )Syntactic חלק מהביטויים שראינו ב- scheme ניתנים להמרה על-ידי המנתח לביטוי אחר, שקול להם, בשביל הערכתו. ביטויים מסוג זה נקראים ביטויים נגזרים או סוכר תחבירי. מדוע כדאי לתרגם ביטויים מצורה א' לביטויים שקולים מצורה ב' ולא להעריך ביטויים מסוג א' באופן ישיר? למשל מפני שזה מותיר את ה- core קטן יותר. מתי לא נוכל להוסיף ביטוי חדש כביטוי נגזר? כאשר לא ניתן לתאר את הביטוי ואת החישוב שלו על-ידי ביטויים קיימים: בין אם פונקציות פרימיטיביות,,special forms ביטויי הפעלה, פרוצדורות, ושילובים ביניהם. נמשיך את הדוגמה של case ונבדוק אם ואיך אפשר להגדיר אותו כביטוי נגזר. דרך ראשונה: לפי חוק החישוב שהגדרנו ל- case, ביטוי השליטה e מחושב פעם אחת לפני בדיקת התנאים. לפי הגזירה הנ"ל e יחושב מספר פעמים. כדי לפתור בעיה זאת נראה דרך נוספת: האם התיקון פותר את כל המקרים? לא. (case e (1 (dosometing)) (2 (* e 2)) (else e)) (case e (1 (dosometing)) (2 (* e 2)) (else e)) (if (= e 1) (dosometing) (if (= e 2) (* e 2) e))) (let ((ex e)) (if (= ex 1) (dosometing) (if (= ex 2) (* ex 2) ex))) אמנם בסמנטיקה של,applicative eval ביטוי השליטה e יחושב לפני הפעלתו על הפרוצדורה ולכן צורה זה מתאימה לחוק החישוב שקבענו. לעומת זאת, בסמנטיקה של,normal eval גם גזירה זאת לא תתאים לחוק החישוב. לכן עבור case לא ניתן בשתי השיטות שראינו לגזור אותו לביטוי פשוט יותר מבלי לבצע חישובים מרובים של ביטוי השליטה. באינטרפטר, גזירה של ביטויים מתבצעת על-ידי הפניה הדדית בין הפרוצדורות derive ל- shallow-derive, כאשר shallow-derive מכילה את כללי הגזירה ה"בסיסיים" ו- derive דואגת להפעילם רקורסיבית עד אשר הביטוי מגיע לנקודת שבת: (define derive (lambda (exp) (if (atomic? exp) exp (let ((derived-exp (let ((mapped-derive-exp (map derive exp))) (if (not (derived? exp)) mapped-derive-exp (shallow-derive mapped-derive-exp))))) (if (equal? exp derived-exp) exp (derive derived-exp)))))) פרוצדורת derive פועלת על כל תת ביטוי בביטוי נתון. כשמסיימת לעבור על תתי הביטויים מפעילה את shallow-derive על הביטוי שחזר. 11

12 כדי לטפל בביטויים שנגזרים לביטויים נגזרים הגזירה הבאה של הביטוי שווה לביטוי עצמו. אחרים )לדוגמה: derive )let*,let מפעילה את עצמה על ביטוי מסוים עד אשר לכן, על מנת להוסיף ביטוי נגזר לשפה, יש לשנות את ה- ASP באופן הבא: הוספת הביטוי ל- shallow-derive - כתיבת התרגום מהביטוי הנגזר לביטוי הגזור )הבסיסי( - הוספת הביטוי ל-? derived - מבני נתונים )ds.rkt( מודול זה מכיל שלושה חלקים: )1( מבני הנתונים שהמעריך משתמש בהם לצורך החישוב של ביטויים החזקת הסביבה הגלובלית )3( מימוש של פרוצדורות ה- substitution. לאחר הערכתם. )2( מבני נתונים: מרכיב זה של המעריך מממש את מבני הנתונים שהמעריך משתמש בהם לצורך החישוב של ביטויים לאחר הערכתם. לדוגמה, נצטרך מבנה נתונים לתיאור פרוצדורות מכיוון שבשלב ההחלפה צריך לשלוף חלקים של הפרוצדורה המחושבת. דוגמה למבנה הנתונים של פרוצדורה: Signature: make-procedure (parameters, body) Type: [List(Symbol) * List -> Lambda-Expression] Signature: procedure-parameters (exp) Type: [Lambda-Expression -> List(Symbol)] Signature: procedure-body (exp) Type: [Lambda-Expression -> List] Signature: compound-procedure?(exp) Type: [T -> Boolean] הסביבה הגלובלית: בנוסף לכך, במודל זה מוחזקת הסביבה הגלובלית בה נשמרים הקישורים בין משתנים לבין ערכים וגם הפרוצדורות הפרימיטיבית. הפרוצדורה מייצרת החלפה )make-sub( בין רשימה של סימנים שמייצגים את הפרוצדורות הפרימיטיביות לבין הערכים ש- scheme נותן לנו לפרוצדורות הפרימיטיביות. נסתכל על סביבה הגלובלית: (define make-the-global-environment (lambda () (let* ((primitive-procedures (list (list 'car car) (list 'cdr cdr) (list 'cons cons) (list 'null? null?) (list '+ +) (list '* *) (list '/ /) (list '> >) (list '< <) (list '- -) (list '= =) 12

13 (list 'list list) (list 'append append) )) (prim-variables (map car primitive-procedures)) (prim-values (map (lambda (x) (make-primitive-procedure (cadr x))) primitive-procedures)) ) (make-sub prim-variables prim-values)))) למשל, נסתכל על הפרוצדורה אשר מחפשת ערך בסביבה: (define get-value-of-variable (lambda (sub var) (letrec ((lookup (lambda (vars vals) (cond ((or (empty-sub? sub) (not (member var vars))) (error 'get-value-of-variable)) ((eq? var (car vars)) (car vals)) (else (lookup (cdr vars) (cdr vals))))))) (lookup (get-variables sub) (get-values sub))))) :Substitution adt במודול זה גם מוגדר ה- ADT עבור פעולת ה- substitution. הממשק המלא מצורף כחומר עזר לעיון בסוף התרגול. אנחנו נתבונן במימוש של הפונקציה :substitute ; Signature: substitute(sub,expr) ; Purpose: Consistent replacement of all FREE ; occurrences of the substitution variables in ; 'expr' by the values of the substitution, respectively. ; 'expr' is a Scheme expression. ; Client Type: [Substitution * ; Scheme-expression union Evaluator-value -> ; Scheme-expression union Evaluator-value] ; Supplier Type: [List*(List union Symbol) -> List union Symbol] ; Example: (substitute ; (make-sub '(x y z) '(3 4 (lambda (x1)(+ x1 1)))) ; '(lambda (x2)(+ x2 y))) ) ==> (lambda (x2)(+ x2 4)) ; Pre-conditions: (1) substitution is not performed on ; 'define' or 'let' expressions or on ; expressions containing such sub-expressions. ; (2) 'expr' is already renamed. Therefore, ; the substitution variables have no bound ; occurrences in expr. (define substitute (lambda (expr variables values) (if (evaluator-value? expr) (substitution-application-to-value (make-sub variables values) expr) (substitution-application-to-expr (make-sub variables values) expr)))) (define substitution-application-to-expr (lambda (sub expr) 13

14 (cond ((empty-sub? sub) expr) ((or (number? expr) (boolean? expr) (quoted? expr)) expr) (else (let ((vars (get-variables sub)) (values (get-values sub))) (cond ((variable? expr) (if (member expr vars) (get-value-of-variable sub expr (lambda () expr)) expr)) (else ; expression is a list of ; expressions, lambda, application, cond. (map (lambda (e) (substitute e vars values)) expr)))))))) (define substitution-application-to-value (lambda (sub val-expr) (let ((vars (get-variables sub)) (values (get-values sub))) (cond ((primitive-procedure? val-expr) val-expr) ((value? val-expr) (if (atomic? (value-content val-expr)) val-expr (make-value (map (lambda (e) (substitute e vars values)) (value-content val-expr))))) ((compound-procedure? val-expr) (make-procedure (procedure-parameters val-expr) (map (lambda (e) (substitute e vars values)) (procedure-body val-expr)))))))) נספח לעיון ADT עבור.substitution ; Signature: make-sub(variables, values) ; Purpose: Create a substitution in which the i-th element of 'variables' ; is mapped to the i-th element of 'values'. ; Client Type: [List(Symbol) * List(Scheme-expression union Evaluator-value) -> ; Substitution] ; Supplier Type: [List(Symbol) * List(List union Symbol)) -> List] ; Example: (make-sub '(x y z) ; '(3 #t (lambda (x) (+ x 1))) ) ==> ; ((x y z) (3 #t (lambda (x) (+ x 1)))) ; Pre-condition: (length variables) = (length values) ; Signature: get-variables(sub) ; Type: Client view: [Sub -> List(Var)] ; Supplier view: [List -> List(Symbol)] ; Signature: get-values(sub) ; Type: Client view: [Sub -> List(Scheme-expression 14

15 ; union Evaluator-value)] ; Supplier view: [List -> List(List union Symbol)] ; Signature: get-value-of-variable(sub,var,fail-cont) ; Type: Client view: [Sub * Var * [Empty -> T] -> ; Scheme-expression union Evaluator-value] ; Supplier view: [List * Symbol * [Empty -> T] -> List union Symbol] ; Purpose: If sub is an empty substitution or does not ; includes var, fail-cont is applied. ; Signature: extend-sub(sub,var,value) ; Purpose: Adds var-value binding, if var is not ; already in sub. Otehrwise -- error ; Client Type: [Substitution * Var * ; Scheme-expression union Evaluator-value -> Substitution] ; Supplier Type: [List * Symbol * List union Symbol -> List] ; Signature: sub?(sub) ; Type: [T -> Boolean] ; Example: (sub? (make-sub '(x y z) ; '(3 #t (lambda (x)(+ x 1))) ) ==> #t ; (sub? (make-sub (list) (list))) ==> #t ; (sub? '()) ==> #f ; Signature: empty-sub?(sub) ; Type: [T -> Boolean] ; Example: (empty-sub? (make-sub '(x y z) ; (list 'Number 'Boolean ; '(3 #t (lambda (x)(+ x 1))) ) ==> #f ; (empty-sub? (make-sub (list) (list))) ==> #t ; Pre-condition: (sub? sub) ; Signature: non-empty-sub?(sub) ; Type: [T -> Boolean] ; Example: (non-empty-sub? (make-sub '(x y z) ; '(3 #t (lambda (x)(+ x 1))) ) ==> #t ; (non-empty-sub? (make-sub (list) (list))) ==> #f ; Pre-condition: (sub? sub) ; Signature: sub-equal?(sub1,sub2) ; Type: Client view: [Sub * Sub -> Boolean] ; Supplier view: [List * List -> Boolean] ; Example: (sub-equal? (make-sub '(x y z) ; '(3 #t (lambda (x)(+ x 1))) ) ; (make-sub '(y x z) ; '(#t 3 (lambda (x)(+ x 1))) ) ) ==> #t ; (sub-equal? (make-sub (list) (list)) (make-sub ; (list) (list)) ) ==> #t 15

עקרונות שפות תכנות 2018 תרגול 8 Type Inference System

עקרונות שפות תכנות 2018 תרגול 8 Type Inference System עקרונות שפות תכנות 2018 תרגול 8 Type Inference System Type Inference System The Type Inference System is a TypeScript Implementation of the algorithm for Type Checking and Inference using Type Equations.

More information

מבוא לתכנות ב- JAVA תרגול 7

מבוא לתכנות ב- JAVA תרגול 7 מבוא לתכנות ב- JAVA תרגול 7 רקורסיה - הקדמה הגדרה רקורסיבית: חדר הוא מסודר אם צד שמאל שלו מסודר שלו מסודר. וצד ימין שיטת פתרון רקורסיבית: פתרון מופעים פשוטים יותר של בעיה בכדי לפתור את הבעיה המקורית רקורסיה

More information

<exp> ::= <define> <cexp> <define> ::= ( define <var-decl> <cexp> ) / DefExp(var:VarDecl, val:cexp)

<exp> ::= <define> <cexp> <define> ::= ( define <var-decl> <cexp> ) / DefExp(var:VarDecl, val:cexp) הנחיות כלליות: תאריך הבוחן: 10.5.2018 שם המרצה: מני אדלר,מיכאל אלחדד, ירון גונן מבחן בקורס: עקרונות שפות תכנות מס' קורס: 202-1-2051 מיועד לתלמידי: מדעי המחשב והנדסת תוכנה שנה: ב' סמסטר: ב' משך הבוחן: 2

More information

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions מבוא לתיכנות עם MATLAB 234127 Lecture 5: Boolean logic and Boolean expressions Written by Prof. Reuven Bar-Yehuda, Technion 2013 Based on slides of Dr. Eran Eden, Weizmann 2008 1 >>g = [89 91 80 98]; >>p

More information

Algorithms. Intro2CS week 5

Algorithms. Intro2CS week 5 Algorithms Intro2CS week 5 1 Computational problems A computational problem specifies an inputoutput relationship What does the input look like? What should the output be for each input? Example: Input:

More information

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions מבוא לתיכנות עם MATLAB 23427 Lecture 5: Boolean logic and Boolean expressions Written by Prof. Reuven Bar-Yehuda, Technion 203 Based on slides of Dr. Eran Eden, Weizmann 2008 ביטויים לוגיים דוגמא: תקינות

More information

סכום (סדרת ערכים) אחרת - דוגמא: סכום-ספרות (num) אם < 10 num החזר 1 או אם = 0 = num החזר 0 public static int numofdigits (int num)

סכום (סדרת ערכים) אחרת - דוגמא: סכום-ספרות (num) אם < 10 num החזר 1 או אם = 0 = num החזר 0 public static int numofdigits (int num) 1 תבנית צבירה תבניות אלגוריתמיות לפעולות רקורסיביות תבנית צבירה לסדרת ערכים: סכום (סדרת ערכים) החזר את ערך הקצה + סכום (סדרת הערכים ללא ערך הקצה) דוגמא: פעולה המחזירה את סכום הספרות שבמספר שלם לא שלילי

More information

מבוא לתכנות ב- JAVA מעבדה 2

מבוא לתכנות ב- JAVA מעבדה 2 מבוא לתכנות ב- JAVA מעבדה 2 מה בתרגול טיפוסים פרימיטיביים המרות טיפוסים אופרטורים יחסיים ולוגיים משפט if-else בתרגול הקודם טיפוסים פרימיטביים לייצוג מספרים שלמים וממשיים ואופרטורים לפעולות בין מספרים.1

More information

הנכות 1 תואיגש םע תודדומתהו תואלול,םי : כרעמ 2 לוגרת

הנכות 1 תואיגש םע תודדומתהו תואלול,םי : כרעמ 2 לוגרת תוכנה 1 תרגול 2: מערכים, לולאות והתמודדות עם שגיאות מערכים מערכים Array: A fixed-length data structure for storing multiple values of the same type Example from last week: An array of odd numbers: Indices

More information

הנכות 1 תואיגש םע תודדומתהו תואלול,םיכרעמ : לו 2 גרת

הנכות 1 תואיגש םע תודדומתהו תואלול,םיכרעמ : לו 2 גרת תוכנה 1 תרגול 2: מערכים, לולאות והתמודדות עם שגיאות מערכים Array: A fixed-length data structure for storing multiple values of the same type Example from last week: An array of odd numbers: Indices (start

More information

מחרוזות ב Java ותכנות מונחה בדיקות )Test Driven Development(

מחרוזות ב Java ותכנות מונחה בדיקות )Test Driven Development( מחרוזות ב Java ותכנות מונחה בדיקות )Test Driven Development( תוכנה 1 תרגול 8 String Immutability Strings are constants String s = " Tea "; s = s.trim(); s = s.replace('t', 'S'); s 1 2 3 " Tea " "Tea" "Sea"

More information

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions מבוא לתיכנות עם MATLAB 234127 Lecture 5: Boolean logic and Boolean expressions Written by Prof. Reuven Bar-Yehuda, Technion 2013 Based on slides of Dr. Eran Eden, Weizmann 2008 1 motivation Proper academic

More information

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Array Creation and Initialization. Loop through Arrays

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Array Creation and Initialization. Loop through Arrays מערכים Array: A fixed-length data structure for storing multiple values of the same type תוכנה 1 Example: An array of odd numbers: Indices (start from 0) 0 1 2 3 4 5 6 7 odds: 1 3 5 7 9 11 13 15 odds.length

More information

הנכות 1 םוכיס לוגרת 13 1

הנכות 1 םוכיס לוגרת 13 1 תוכנה 1 סיכום תרגול 13 1 קצת על מנשקים מנשק יכול להרחיב יותר ממנשק אחד שירותים במנשק הם תמיד מופשטים וציבוריים public interface MyInterface { public abstract int foo1(int i); int foo2(int i); The modifiers

More information

תוכנה 1 3 תרגול מס' מערכים ומבני בקרה

תוכנה 1 3 תרגול מס' מערכים ומבני בקרה תוכנה 1 3 תרגול מס' מערכים ומבני בקרה מערכים Array: A fixed-length data structure for storing multiple values of the same type Example: An array of odd numbers: Indices (start from 0) 0 1 2 3 4 5 6 7 odds:

More information

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Loop through Arrays. Array Creation and Initialization

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Loop through Arrays. Array Creation and Initialization מערכים תוכנה 1 Array: A fixed-length data structure for storing multiple values of the same type Example from last week: An array of odd numbers: Indices (start from 0) 0 1 2 3 4 5 6 7 תרגול 2: מערכים

More information

ASP.Net Web API.

ASP.Net Web API. ASP.Net Web API 1 מה זה? Web API View בלבד ולא Data אותו מממש השרת והוא מחזיר לקליינט API הוא Web API הבקשה והתשובה הן בפרוטוקול Http\Https הקליינטים של Web API יכולים להיות רבים : אפשר להשתמש גם בMVC

More information

דף הדרכה ליצירת שרת/ לקוח עם GUI

דף הדרכה ליצירת שרת/ לקוח עם GUI דף הדרכה ליצירת שרת/ לקוח עם GUI בשיעורים הקודמים למדנו כיצד ליצור שרת לקוח פשוט, ויצירת טופס המכיל פקדים כלומר יצירת GUI למשתמש, בשיעור זה נרצה להראות את הדרך לשילוב בין השניים כלומר ליצור לקוח client

More information

משתנים שעור מס. 2 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

משתנים שעור מס. 2 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1 משתנים שעור מס. 2 דרור טובי דר' כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1 תפקיד המשתנים הצהרה על משתנה השמת ערך במשתנה int a, b, c; a = 1234; b = 99; c = a + b; משתנים מאפשרים לנו לשמור

More information

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

More information

מבוא למדעי המחשב תרגול 8 רשימה משורשרת כללית, Comparator

מבוא למדעי המחשב תרגול 8 רשימה משורשרת כללית, Comparator מבוא למדעי המחשב 2017 תרגול 8 רשימה משורשרת כללית, Comparator בתרגול היום. LinkedList בניית ההכללה מ- LinkIntList תרגול המבנה ושימושיו ממשקים: Comparator Sorted Linked List ל- LinkedList ע"י שימוש ב- Comparator

More information

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Scheme in Scheme: The Metacircular Evaluator Eval and Apply Scheme in Scheme: The Metacircular Evaluator Eval and Apply CS21b: Structure and Interpretation of Computer Programs Brandeis University Spring Term, 2015 The metacircular evaluator is A rendition of Scheme,

More information

Amortized Analysis, Union-Find,

Amortized Analysis, Union-Find, Practical Session No. 13 Amortized Analysis, Union-Find, AMORTIZED ANALYSIS Refers to finding the average running time per operation, over a worst-case sequence of operations. Amortized analysis differs

More information

תוכנה 1 תרגול 2: מערכים ומבני בקרה

תוכנה 1 תרגול 2: מערכים ומבני בקרה תוכנה 1 תרגול 2: מערכים ומבני בקרה 2 Useful Eclipse Shortcuts Ctrl+1 quick fix for errors, or small refactoring suggestions Ctrl+SPACE code content assist (auto-completion) Auto completion for main create

More information

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter 6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,

More information

Communication Networks ( ) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University. Allon Wagner

Communication Networks ( ) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University. Allon Wagner Communication Networks (0368-3030) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University Allon Wagner Kurose & Ross, Chapter 3.5.5, 3.7 (5 th ed.) Many slides adapted from: J. Kurose

More information

תוכנה 1 תרגול 2: מערכים, מבני בקרה ושגיאות

תוכנה 1 תרגול 2: מערכים, מבני בקרה ושגיאות תוכנה 1 תרגול 2: מערכים, מבני בקרה ושגיאות מערכים Array: A fixed-length data structure for storing multiple values of the same type Example: An array of odd numbers: Indices (start from 0) 0 1 2 3 4 5

More information

Practical Session - Heap

Practical Session - Heap Practical Session - Heap Heap Heap Maximum-Heap Minimum-Heap Heap-Array A binary heap can be considered as a complete binary tree, (the last level is full from the left to a certain point). For each node

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,

More information

מבוא לתכנות ב- JAVA תרגול 6

מבוא לתכנות ב- JAVA תרגול 6 מבוא לתכנות ב- JAVA תרגול 6 מה בתרגול )methods( פונקציות/שיטות ב- Java הגדרת פונקציה קריאה/הפעלה העברת ארגומנטים ערכי החזרה מבוא לפונקציות- שימוש חוזר בקוד נניח שבמהלך תוכנית נדרשתם לחשב את הסכום של המספרים

More information

מבוא למדעי המחשב תירגול 2: מבוא למדעי המחשב מ' - תירגול 2

מבוא למדעי המחשב תירגול 2: מבוא למדעי המחשב מ' - תירגול 2 מבוא למדעי המחשב תירגול 2: לולאות, קלט, וטיפוסים 1 תוכנייה לולאת while קלט טיפוסי משתנים המרת טיפוסים טיפוס char 2 לולאת while 3 לולאת while קטע קוד מתבצע שוב ושוב כל עוד תנאי מתקיים int number = 40; while(number>0)

More information

תרגול 4 פונקציות. מבנה של פונקציה: public static <return value type> <function name> (<arg1 type> <arg1>, <arg2 type> <arg2>, ) { <function body> }

תרגול 4 פונקציות. מבנה של פונקציה: public static <return value type> <function name> (<arg1 type> <arg1>, <arg2 type> <arg2>, ) { <function body> } נושאי התרגול: מה הן פונקציות הגדרת פונקציה,קריאה לפונקציה העברת ארגומנטים,החזרת ערך או void העברת משתנים פרימיטיביים ומערכים לפונקציה העמסה של פונקציות תרגול 4 פונקציות מוטיבציה לעיתים,אנו נזקקים לבצע

More information

מבוא למדעי המחשב תרגול 12 מחסנית )Stack( memoization

מבוא למדעי המחשב תרגול 12 מחסנית )Stack( memoization מבוא למדעי המחשב 2017 תרגול 12 מחסנית )Stack( memoization בתרגול היום מחסנית בדיקת איזון סוגריים בביטוי אריתמטי משולש Pascal לא רקורסיבי memoization דוגמאות שימוש: בעיית העודף תזכורת: מחסנית :)stack( מבנה

More information

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 1996 Lecture Notes { October 31,

More information

מבוא למדעי המחשב תירגול 3:

מבוא למדעי המחשב תירגול 3: מבוא למדעי המחשב תירגול 3: לולאות, קלט, וטיפוסים תוכנייה לולאת while קלט טיפוסי משתנים המרת טיפוסים טיפוס char מבוא למדעי המחשב מ' - תירגול 3 2 לולאת while מבוא למדעי המחשב מ' - תירגול 3 3 לולאת while

More information

- MEAN Stack חזרה. MongoDB - as the database Express - as the web framework AngularJS - as the frontend framework NodeJS- as the server platform

- MEAN Stack חזרה. MongoDB - as the database Express - as the web framework AngularJS - as the frontend framework NodeJS- as the server platform הדר פיקאלי תשע"ו - 2016 - MEAN Stack חזרה בניית web applications כרוכה בשימוש בטכנולוגיות וכלים שונים, להתמודדות עם: מסד נתונים, פעולות בצד השרת, טיפול בצד הלקוח והצגה של הנתונים. מהו?MEAN "MEAN is a fullstack

More information

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Programming Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

More information

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator. .00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do

More information

תוכנה 1 תרגול מספר 13

תוכנה 1 תרגול מספר 13 1 תוכנה 1 תרגול מספר 13 ו- HashCode Equals עוד על טיפוסים מוכללים )Advanced Generics( חריגים )Exceptions( בית הספר למדעי המחשב אוניברסיטת תל אביב 1 2 ו- HASHCODE EQUALS 3 תזכורת: המחלקה Object package

More information

תוכנה 1 תרגול מספר 13

תוכנה 1 תרגול מספר 13 1 2 תוכנה 1 תרגול מספר 13 ו- HashCode Equals עוד על טיפוסים מוכללים )Advanced Generics( ו- HASHCODE EQUALS חריגים )Exceptions( בית הספר למדעי המחשב אוניברסיטת תל אביב 1 3 4 package java.lang; תזכורת: המחלקה

More information

שאלה 1 מהו הפלט של התוכנית הבאה:

שאלה 1 מהו הפלט של התוכנית הבאה: תרגול חזרה שאלה 1 מהו הפלט של התוכנית הבאה: public sttic int wht(int n) { int i; int sum=0; if(n == 0) return 1; for (i=0; i

More information

תוכנה 1 תרגול מספר 10: תרגיל חזרה חברת הייטק בית הספר למדעי המחשב אוניברסיטת תל אביב

תוכנה 1 תרגול מספר 10: תרגיל חזרה חברת הייטק בית הספר למדעי המחשב אוניברסיטת תל אביב 1 תוכנה 1 תרגול מספר 10: תרגיל חזרה חברת הייטק בית הספר למדעי המחשב אוניברסיטת תל אביב 1 2 חברת הייטק בתרגיל זה נתרגל מספר נושאים אותם למדנו בשיעורים האחרונים: עיצוב ובניית מודל המורכב ממחלקות לתיאור סביבה

More information

Engineering Programming A

Engineering Programming A Engineering Programming A תרגול 5 25.11.2012 מערכים חד-מימדיים )תזכורת( לדוגמא: מערך בשם Arr בגודל 8 שאיבריו מטיפוס int 3 7 5 6 8 1 23 16 0 1 2 3 4 5 6 7 ב - arr[0] ב יושב ערך שהוא המספר השלם 3 arr[1]

More information

Introduction to Programming in C תרגול 8

Introduction to Programming in C תרגול 8 Introduction to Programming in C תרגול 8 1 1 נושאים מצביעים רקע אופרטורים על מצביעים מצביעים כפרמטרים לפונקציה הקצאה דינמית מבנים תאור הזיכרון של המחשב: מצביעים ניתן לחשוב על זיכרון המחשב כעל רצף של תאים,

More information

Practical Session No. 14 Topological sort,amortized Analysis

Practical Session No. 14 Topological sort,amortized Analysis Practical Session No. 14 Topological sort,amortized Analysis Topological- Sort Topological sort Ordering of vertices in a directed acyclic graph (DAG) G=(V,E) such that if there is a path from v to u in

More information

תוכנה 1 סמסטר א' תשע"א

תוכנה 1 סמסטר א' תשעא General Tips on Programming תוכנה 1 סמסטר א' תשע"א תרגול מס' 6 מנשקים, דיאגרמות וביטים * רובי בוים ומתי שמרת Write your code modularly top-down approach Compile + test functionality on the fly Start with

More information

$ gcc check.c. $ a.out. $ gcc check.c -o check. $ check. $ gcc -Wall check.c -o check. #include <stdio.h>

$ gcc check.c. $ a.out. $ gcc check.c -o check. $ check. $ gcc -Wall check.c -o check. #include <stdio.h> תכנות בסיסי בשפת C תוכנית ראשונה תוכנית ב - C מורכבת מאוסף פונקציות והגדרות טיפוסים. C איננה שפה object oriented כך שאין בה מושגים של מחלקה ואובייקט. נתחיל בתוכנית הראשונה המסורתית, זו המדפיסה הודעה יחידה

More information

הנכות 1 םוכיס לוגרת 13 1

הנכות 1 םוכיס לוגרת 13 1 תוכנה 1 סיכום תרגול 13 1 בחינה באופק! הבחינה תכלול את כל הנושאים שכיסינו במהלך הסמסטר: כל ההרצאות כל תרגולים כל תרגילי בית חומר סגור שאלות אמריקאיות 2 קצת על מנשקים מנשק יכול להרחיב שירותים במנשק הם תמיד

More information

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation

More information

מבוא לתכנות בשפת C. Tzachi (Isaac) Rosen

מבוא לתכנות בשפת C. Tzachi (Isaac) Rosen מבוא לתכנות בשפת C מצביעים והקצאה דינאמית כתובות של משתנים לכל משתנה כתובת של המקום שלו בזיכרון כבר ראינו: שם של מערך הוא למעשה הכתובת של התא הראשון )באינדקס 0( של המערך להזכירכם: תא של מערך הינו משתנה

More information

קורס תכנות שיעור שני: שימוש במשתנים,

קורס תכנות שיעור שני: שימוש במשתנים, קורס תכנות שיעור שני: שימוש במשתנים, בקרת זרימה, לולאות 1 נושאי השיעור היום משתנים )variables( טיפוסי משתנים בשפת C הגדרת משתנים השמה למשתנים פעולות על משתנים קליטת ערכים מהמשתמש הדפסה משתנים בקרת זרימה

More information

מבוא לתכנות ב- JAVA תרגול 5. Ipc161- practical session 5

מבוא לתכנות ב- JAVA תרגול 5. Ipc161- practical session 5 מבוא לתכנות ב- JAVA תרגול 5 Ipc161- practical session 5 מה בתרגול מערכים דו ממדיים )methods( פונקציות/שיטות ב- Java הגדרת פונקציה קריאה/הפעלה העברת ארגומנטים ערכי החזרה מערך דו ממדי מערך של מערכים חד ממדיים

More information

מצליחה. 1. int fork-bomb() 2. { 3. fork(); 4. fork() && fork() fork(); 5. fork(); printf("bla\n"); 8. return 0; 9. }

מצליחה. 1. int fork-bomb() 2. { 3. fork(); 4. fork() && fork() fork(); 5. fork(); printf(bla\n); 8. return 0; 9. } שאלה : (4 נקודות) א. ב. ג. (5 נקודות) הגדירו את המונח race-condition במדוייק לא להשמיט פרטים. ספקו דוגמא. (5 נקודות) מהו? Monitor נא לספק הגדרה מלאה. ( נקודות) ( נקודות) ציינו כמה תהליכים יווצרו בקוד הבא

More information

תוכנה 1 בשפת Java נושאים שונים בהורשה רובי בוים ומתי שמרת בית הספר למדעי המחשב אוניברסיטת תל אביב

תוכנה 1 בשפת Java נושאים שונים בהורשה רובי בוים ומתי שמרת בית הספר למדעי המחשב אוניברסיטת תל אביב תוכנה 1 בשפת Java נושאים שונים בהורשה רובי בוים ומתי שמרת בית הספר למדעי המחשב אוניברסיטת תל אביב Today Static vs. Dynamic binding Equals / hashcode String Immutability (maybe) 2 Static versus run-time

More information

תור שימושים בעולם התוכנה

תור שימושים בעולם התוכנה מבוא למדעי המחשב הרצאה : Queue, Iterator & Iterable תור מבנה נתונים אבסטרקטי תור שימושים בעולם התוכנה השימושים של תורים בעולם התוכנה מזכירים מאוד תורים במציאות: )VoIP( )YouTube( מקלדת שידור סרט באינטרנט

More information

תוכנה 1. תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes

תוכנה 1. תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes תוכנה 1 תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes class Outer { static class NestedButNotInner {... class Inner {... מחלקות מקוננות NESTED CLASSES 2 מחלקה מקוננת Class) )Nested

More information

רשימות דילוגים Skip Lists

רשימות דילוגים Skip Lists Lecture6 of Geiger & Itai s slide brochure www.cs.technion.ac.il/~dang/courseds רשימות דילוגים Skip Lists Skip lists: A probabilistic Alternative to Balanced Trees, William Pugh, Communications of the

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

Computer Programming A תרגול 9

Computer Programming A תרגול 9 Computer Programming A תרגול 9 1 מטרת התרגול הקצאת זיכרון מבנים רשימות דינאמית ניהול הזיכרון בתוכנית עד כה כל המשתנים שראינו היו לוקאליים. משך הקיום של משתנים מקומיים הוא הזמן אשר הפונקציה בה הם נמצאים

More information

פתרון מוצע לבחינת מה"ט ב_שפת c מועד אביב תשע"ח, פברואר 8102 מחבר: מר שייקה בילו, מכללת אורט רחובות

פתרון מוצע לבחינת מהט ב_שפת c מועד אביב תשעח, פברואר 8102 מחבר: מר שייקה בילו, מכללת אורט רחובות פתרון מוצע לבחינת מה"ט ב_שפת c מועד אביב תשע"ח, פברואר 8102 מחבר: מר שייקה בילו, מכללת אורט רחובות שאלה מספר 1 התוכנית מגדירה חמישה משתנים שלמים: השלושה הראשונים הם שלושה מצביעים - *s *t,i. j ושלושה נוספים

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Slides by Dana Fisman based on book by Mira Balaban and lecuture notes by Michael Elhadad Lesson 15 Type Inference Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

תרגילים ופתרונות בשפת - C הסתעפויות

תרגילים ופתרונות בשפת - C הסתעפויות תרגילים ופתרונות בשפת - C הסתעפויות כתב וערך: שייקה בילו תרגיל - 1 כתוב תוכנית שתקבל מהמשתמש שלושה מספרים, ותציג את הגדול מביניהם על המסך. #include void main() int mis1, mis2, mis3, max; printf("please

More information

ב ה צ ל ח ה! אוניברסיטת בן גוריון בנגב מספר נבחן : תאריך המבחן: כ"ה תשרי תשע"ח 15/10/17 שמות המורים: ציון סיקסיק מיועד לתלמידי : א'

ב ה צ ל ח ה! אוניברסיטת בן גוריון בנגב מספר נבחן : תאריך המבחן: כה תשרי תשעח 15/10/17 שמות המורים: ציון סיקסיק מיועד לתלמידי : א' אוניברסיטת בן גוריון בנגב מספר נבחן : תאריך המבחן: כ"ה תשרי תשע"ח 15/10/17 שמות המורים: ציון סיקסיק א' ב- C תכנות מבחן ב: 202-1-9011 מס' הקורס : הנדסה מיועד לתלמידי : א' מועד קיץ סמ' שנה תשע"ז 3 שעות משך

More information

מערכים שעור מס. 4 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

מערכים שעור מס. 4 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1 מערכים שעור מס. 4 דרור טובי דר' כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1 למה מערכים? ברצוננו לאחסן בתוכנית ציוני בחינה כדי לחשב את ממוצע הציונים וסטיית התקן. נניח ש 30 סטודנטים לקחו

More information

CS61A Midterm 2 Review (v1.1)

CS61A Midterm 2 Review (v1.1) Spring 2006 1 CS61A Midterm 2 Review (v1.1) Basic Info Your login: Your section number: Your TA s name: Midterm 2 is going to be held on Tuesday 7-9p, at 1 Pimentel. What will Scheme print? What will the

More information

תרגול מספר 3: מערכים

תרגול מספר 3: מערכים היום בתרגול:.1.2 תרגול מספר 3: מערכים מערך חד-מימדי: מה זה מערך ולמה צריך אותו? איך מגדירים? איך זה נראה בזכרון? דוגמאות לשימוש במערך חד-מימדי השוואה בין משתנה פרימיטיבי למשתנה שאינו פרימיטיבי מערך דו-מימדי:

More information

קורס תכנות בשיעור הקודם למדנו על רקורסיה שיעור שישי: מערכים פונקציה רקורסיבית שאלה חישוב נוסחאות רקורסיביות בשפת C

קורס תכנות בשיעור הקודם למדנו על רקורסיה שיעור שישי: מערכים פונקציה רקורסיבית שאלה חישוב נוסחאות רקורסיביות בשפת C בשיעור הקודם למדנו על רקורסיה פתרנו את בעיית מגדלי הנוי בעזרת רקורסיה כלומר בעזרת פונקציה שקוראת לעצמה. רקורסיה מאפשרת לנו לפתור בעיה "גדולה" בעזרת פתרון של בעיות "קטנות" המרכיבות אותה. קורס תכנות שיעור

More information

ת ונכת סרוק תורשוקמ תומישר :יעישת רועיש 1

ת ונכת סרוק תורשוקמ תומישר :יעישת רועיש 1 קורס תכנות שיעור תשיעי: רשימות מקושרות 1 הקצאה דינאמית של מערכים דו-ממדיים )לפחות( שלוש גישות אפשריות:.1 מערך של מערכים מצביעים לתוך מערך "גדול".2 3. מצביע יחיד למערך גדול 2 The Interface 3 (Simple) Usage

More information

CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages. Lecture 16 CS 314 Principles of Programming Languages Lecture 16 Zheng Zhang Department of Computer Science Rutgers University Friday 28 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:

More information

Tutorial 10. Introduction to C++ שימו

Tutorial 10. Introduction to C++ שימו Introduction to ++ שימו תרגול זה אינו התרגול הרישמי של הקורס. הוא מבוסס על חוברת התרגולים אך מכיל שינויים, הסברים נוספים ודוגמאות שונות או נוספות. + + תוכנ ית רא שונה ב הכרו ת עם + + תרגול // First ++

More information

Comp 311: Sample Midterm Examination

Comp 311: Sample Midterm Examination Comp 311: Sample Midterm Examination October 29, 2007 Name: Id #: Instructions 1. The examination is closed book. If you forget the name for a Scheme operation, make up a name for it and write a brief

More information

הנכות 1 םוכיס לוגרת 14 1

הנכות 1 םוכיס לוגרת 14 1 תוכנה 1 סיכום תרגול 14 1 קצת על מנשקים מנשק יכול להרחיב יותר ממנשק אחד שירותים במנשק הם תמיד מופשטים וציבוריים public interface MyInterface { public abstract int foo1(int i); int foo2(int i); The modifiers

More information

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want Computer Science 21b (Spring Term, 2017) Structure and Interpretation of Computer Programs Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want Here is one of the big ideas

More information

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal) CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal) What is the Metacircular Evaluator? It is the best part

More information

תוכנה 1 תרגול מספר 10: תרגיל חברת הייטק בית הספר למדעי המחשב אוניברסיטת תל אביב

תוכנה 1 תרגול מספר 10: תרגיל חברת הייטק בית הספר למדעי המחשב אוניברסיטת תל אביב 1 תוכנה 1 תרגול מספר 10: תרגיל חברת הייטק בית הספר למדעי המחשב אוניברסיטת תל אביב 1 2 חברת הייטק בתרגיל זה נתרגל מספר נושאים אותם למדנו בשיעורים האחרונים: עיצוב ובניית מודל המורכב ממחלקות לתיאור סביבה

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Lesson 6 - Defining a Programming Language Bottom Up Collaboration and Management - Elements of Programming Dana Fisman 1 What we accomplished

More information

Nir Adar

Nir Adar שפת Java למתכנתי ++C - חלק שני מסמך זה הורד מהאתר. אין להפיץ מסמך זה במדיה כלשהי, ללא אישור מפורש מאת המחבר. מחבר המסמך איננו אחראי לכל נזק, ישיר או עקיף, שיגרם עקב השימוש במידע המופיע במסמך, וכן לנכונות

More information

פתרון מוצע לבחינה בשפת C של מה"ט מועד אביב תשע"ז, פברואר 2017 מחבר: מר עסאקלה שאדי, מכללת אורט בראודה

פתרון מוצע לבחינה בשפת C של מהט מועד אביב תשעז, פברואר 2017 מחבר: מר עסאקלה שאדי, מכללת אורט בראודה פתרון מוצע לבחינה בשפת C של מה"ט מועד אביב תשע"ז, פברואר 2017 מחבר: מר עסאקלה שאדי, מכללת אורט בראודה שאלה מספר 1 נתונה התכנית הבאה בשפת C: #include #define SUM_OF_3(x,y,z) x+y+z #define AVRG_OF_3(x,y,z)

More information

תכנות מונחה עצמים משחקים תשע"ו

תכנות מונחה עצמים משחקים תשעו move semantics 1 תכנות מונחה עצמים ופיתוח משחקים תשע"ו סמנטיקת ההעברה semantics( )Move move semantics 2 מטרה האצה של התוכניות, שיפור בביצועים על ידי חסכון בבנייה והעתקה של אובייקטים זמניים move semantics

More information

A Brief Introduction to Scheme (II)

A Brief Introduction to Scheme (II) A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data

More information

תוכנה 1 * לא בהכרח בסדר הזה

תוכנה 1 * לא בהכרח בסדר הזה תוכנה 1 תרגול 7: מנשקים, פולימורפיזם ועוד * לא בהכרח בסדר הזה 2 מנשקים מנשקים - תזכורת מנשק )interface( הוא מבנה תחבירי ב- Java המאפשר לחסוך בקוד לקוח. מנשק מכיל כותרות של מתודות )חתימות(. מימוש דיפולטיבי

More information

הנכות 1 םוכיס לוגרת 13 1

הנכות 1 םוכיס לוגרת 13 1 תוכנה 1 סיכום תרגול 13 1 בחינה באופק! הבחינה תכלול את כל הנושאים שכיסינו במהלך הסמסטר: כל ההרצאות כל תרגולים כל תרגילי בית חומר סגור שאלות אמריקאיות 2 קצת על מנשקים מנשק יכול להרחיב יותר ממנשק אחד שירותים

More information

גירסה

גירסה גירסה 1.00 29.02.2004 לולאות בשפת C מסמך זה הורד מהאתר.http://underwar.livedns.co.il אין להפיץ מסמך זה במדיה כלשהי, ללא אישור מפורש מאת המחבר. מחבר המסמך איננו אחראי לכל נזק, ישיר או עקיף, שיגרם עקב השימוש

More information

תוכנה 1 * לא בהכרח בסדר הזה

תוכנה 1 * לא בהכרח בסדר הזה תוכנה 1 תרגול 7: מנשקים, פולימורפיזם ועוד * לא בהכרח בסדר הזה 2 מנשקים מנשקים מנשק )interface( הוא מבנה תחבירי ב- Java המאפשר לחסוך בקוד לקוח. מנשק מכיל כותרות של מתודות המימוש שלהן. )חתימות( ללא קוד אשר

More information

1.3. Conditional expressions To express case distinctions like

1.3. Conditional expressions To express case distinctions like Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a

More information

Programming for Engineers in Python

Programming for Engineers in Python Programming for Engineers in Python Lecture 9: Sorting, Searching and Time Complexity Analysis Autumn 2011-12 1 Lecture 8: Highlights Design a recursive algorithm by 1. Solving big instances using the

More information

Programming for Engineers in Python

Programming for Engineers in Python Programming for Engineers in Python Lecture 9: Sorting, Searching and Time Complexity Analysis Autumn 2011-12 1 Lecture 8: Highlights Design a recursive algorithm by 1. Solving big instances using the

More information

Building a system for symbolic differentiation

Building a system for symbolic differentiation Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

More information

Operating Systems. Practical Session 4 Threads

Operating Systems. Practical Session 4 Threads Operating Systems Practical Session 4 Threads 1 Threads Executed within a process. Allow multiple independent executions under the same process (container). Possible states: running, ready, blocked, terminated.

More information

תזכורת: עץבינארי מבוא למדעי המחשב הרצאה 24: עצי חיפוש בינאריים

תזכורת: עץבינארי מבוא למדעי המחשב הרצאה 24: עצי חיפוש בינאריים מבוא למדעי המחשב הרצאה 2: עצי חיפוש בינאריים תזכורת: עץבינארי בנוסףלרשימהמקושרת ומערך, הצגנומבנהנתונים קונקרטיחדש עץבינארי עץבינארימורכבמ: שורש תת-עץשמאלי תת-עץימני A B C D E F G 2 תזכורת: שורש ותתי-עצים

More information

Overview. CS301 Session 3. Problem set 1. Thinking recursively

Overview. CS301 Session 3. Problem set 1. Thinking recursively Overview CS301 Session 3 Review of problem set 1 S-expressions Recursive list processing Applicative programming A look forward: local variables 1 2 Problem set 1 Don't use begin unless you need a side

More information

Smart Pointers Nir Adar

Smart Pointers Nir Adar 7.1.2005 גירסה 1.00 Smart Pointers מסמך זה הורד מהאתר. אין להפיץ מסמך זה במדיה כלשהי, ללא אישור מפורש מאת המחבר. מחבר המסמך איננו אחראי לכל נזק, ישיר או עקיף, שיגרם עקב השימוש במידע המופיע במסמך, וכן לנכונות

More information

תכנות מתקדם בשפת C משתנים

תכנות מתקדם בשפת C משתנים תכנות מתקדם בשפת C משתנים 1 משתנים סוגי משתנים בשפת C ההבדלים בין סוגי המשתנים השונים 2 /* This program computes m to the power of n */ /* Assumptions: m is an integer; n is a positive integer */ #include

More information

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones. 6.001, Fall Semester, 2002 Quiz II Sample solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 17: Functional Programming Zheng (Eddy Zhang Rutgers University April 4, 2018 Class Information Homework 6 will be posted later today. All test cases

More information

שפת XML הפכה להיות נפוצה ובעלת תפקידים רבים, במיוחד עקב גמישותה הרבה:

שפת XML הפכה להיות נפוצה ובעלת תפקידים רבים, במיוחד עקב גמישותה הרבה: שעור extensible Markup Language - XML 11 XML היא שפת סימון בדומה ל- HTML, גם XML היא שפת סימון. XML איננה שפת תכנות שמאפשרת פיתוח תכניות אשר מבצעות פעולות חישוב. בדומה ל- HTML גם XML מורכבת, למעשה, מתגיות

More information

Exams questions examples

Exams questions examples Exams questions examples 1 Exam example 1. y - x what נק' ( לפניך הפעולה הרקורסיבית מקבלת כפרמטרים שני מספרים שלמים ו 10 )? מה יהיה הפלט כתוצאה מזימון הפעולה what public static int what(int x, int y) if(x

More information

From Syntactic Sugar to the Syntactic Meth Lab:

From Syntactic Sugar to the Syntactic Meth Lab: From Syntactic Sugar to the Syntactic Meth Lab: Using Macros to Cook the Language You Want a V E N E TRUT H Brandeis S PA R T U N T O I T S I N N E R M OS T COMPUTER SCIENCE 21B: Structure and Interpretation

More information